package com.waterstudio.filedownloader.ext.m3u8.tsdownlaoder;

import android.support.annotation.NonNull;
import android.util.Log;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.download.database.TsTask;
import com.liulishuo.filedownloader.util.FileDownloadSerialQueue;
import com.waterstudio.filedownloader.ext.m3u8.BuildConfig;
import com.waterstudio.filedownloader.ext.m3u8.R;
import com.waterstudio.filedownloader.ext.m3u8.TsDownloadHandler;
import com.waterstudio.filedownloader.ext.m3u8.bean.EncKey;

import java.io.File;
import java.util.List;

import javax.Constant;

/**
 * 单线程顺序下载 Ts 流，可以避免占用太多线程，阻塞了其他任务下载
 * <p>
 * Created by chensuilun on 2019/3/17.
 */
public class SerialTsDownloadHandler implements TsDownloadHandler {
    private static final String TAG = "SerialTsDownloadHandler";
    private FileDownloadSerialQueue mSerialQueue;
    private FileDownloadListener mDownloadListener;

    public SerialTsDownloadHandler(@NonNull FileDownloadListener downloadListener) {
        mDownloadListener = downloadListener;
    }

    @Override
    public int prepareDownloadTs(List<TsTask> tsTasks, boolean foreReDownload) {
        BaseDownloadTask tsDownloadTask;
        for (TsTask tsTask : tsTasks) {
            if (new File(tsTask.getTargetPath()).exists() && !foreReDownload) {
                if (BuildConfig.LOG_ENABLE) {
                    Log.v(TAG, String.format("ignore index %d,%s already exist.", tsTask.getIndex(), tsTask.getTargetPath()));
                }
                tsTask.setCurDuration(tsTask.getDuration());
                continue;
            }
            tsDownloadTask = FileDownloader.getImpl().create(tsTask.getUrl());
            tsDownloadTask.setForceReDownload(foreReDownload);
            tsDownloadTask.setListener(mDownloadListener);
            tsDownloadTask.setPath(tsTask.getTargetPath(), false);
            tsDownloadTask.setTag(R.id.is_m3u8_ts_task, true);
            tsDownloadTask.setTag(R.id.ts_index, tsTask.getIndex());
            if (tsTask.getTsKey() != null) {
                tsDownloadTask.setTag(R.id.encrypt, new EncKey(tsTask.getTsKey().getKey(), tsTask.getTsKey().getIv()));
            }
            tsDownloadTask.addHeader(Constant.HEADER_UA, Constant.UA);
            if (mSerialQueue == null) {
                mSerialQueue = new FileDownloadSerialQueue();
                mSerialQueue.pause();
            }
            mSerialQueue.enqueue(tsDownloadTask);
        }
        int size = 0;
        if (mSerialQueue != null) {
            size = mSerialQueue.getWaitingTaskCount();
        }
        return size;
    }

    @Override
    public void startDownload() {
        if (mSerialQueue != null) {
            mSerialQueue.resume();
        }
    }


    @Override
    public void pause() {
        if (mSerialQueue != null) {
            mSerialQueue.pause();
            mSerialQueue.shutdown();
        }
    }
}
